package com.mygdx.game.animation;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

/**
 * Animation
 * http://blog.sina.com.cn/s/blog_940dd50a0101c54b.html
 * Created by 80002023 on 2016/8/3.
 */
public class AnimationTest1 extends Game {

    public static final int FRAME_COLS = 6;

    public static final int FRAME_ROWS = 5;

    Animation walkAnimation;

    Texture walkSheet;

    TextureRegion[] walkFrames;

    SpriteBatch batch;

    TextureRegion currentFrame;

    float stateTime;

    @Override
    public void create() {

        walkSheet = new Texture(Gdx.files.internal("data/animation_sheet.png"));

        TextureRegion[][] temp = TextureRegion.split(walkSheet,
                walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight()
                        / FRAME_ROWS);
        walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];

        int index = 0;

        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {

                walkFrames[index++] = temp[i][j];
            }
        }

        walkAnimation = new Animation(0.025f, walkFrames);

//		walkAnimation.setPlayMode(Animation.LOOP_PINGPONG);

        batch = new SpriteBatch();

        stateTime = 0;
    }

    @Override
    public void render() {

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stateTime += Gdx.graphics.getDeltaTime();
        Gdx.app.log("AnimationTest1", "state time:" + stateTime);

        currentFrame = walkAnimation.getKeyFrame(stateTime, true);

        batch.begin();

        batch.draw(currentFrame, 100, 100);

        batch.end();
    }

    @Override
    public void dispose() {

        walkSheet.dispose();
        batch.dispose();
    }
}
